<?php

	include ('../config/conexao.php');
	include ('../config/validacao.php');
	include ('../modlogin/logger.php');

	if(isset($_GET["id"]) ){
		$rs = mysql_query('SELECT contato_idcontato FROM `ramais_has_contato` WHERE `ramais_idramais` ='.$_GET["id"]);
		$string = '';
		// loop through each zipcode returned and format the response for jQuery
		if ( $rs && mysql_num_rows($rs) )
		{
			while( $row = mysql_fetch_array($rs) )
			{
				$string = $string.$row['contato_idcontato']."|";
				
			}

			echo "<script type='text/javascript' language='Javascript'>
			var resposta = confirm('Existem contatos cadastrados neste ramal. Altera-los de ramal?');
			if (resposta == true) {
				window.location.href = '/grambel/c_contato/editar_contatos.php?string=".$string."';
			}else{
				alert('Ramal nao excluido!')
				window.location.href = 'e_ramal.php';
			}
			</script>";

		}else{
			$sql = "DELETE FROM ramais WHERE idramais='".$_GET['id']."';";
			mysql_query("SET NAMES 'utf8'");
			$retval = mysql_query($sql,$link );
			if(! $retval )
			{
				Logger('Erro ao excluir ramal:'.mysql_error());
				echo "<script>alert('Erro ao excluir ramal: ". mysql_error()."');</script>";
				//echo $sql;
				echo "<script>window.location.href = 'e_ramal.php'";
			}
			else{
				Logger($sql,99);
				echo "<script>alert('Ramal excluido!');</script>";
				echo "<script>window.location.href = 'e_ramal.php';</script>";
			}
		}
	}
?>
